const express = require('express');
const res = express.response;


res.message = function (req, msg, type = 'info') {
    req.session.messages = req.session.messages || [];
    req.session.messages.push({type, msg});
    req.session.save(function (err) {       // express-session 网站上说是自动保存，但是我如果不保存就没有
        if (err) throw err;
    })
};

res.error = function (req, msg) {
    res.message(req, msg, 'error');
};

module.exports = function (req, res, next) {
    res.locals.messages = req.session.messages || [];
    res.locals.removeMessages = function () {
        req.session.messages = [];
    };
    next();
};